package com.repast.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.repast.common.ServerResponse;
import com.repast.service.IOrdersService;
import com.repast.utils.JsonUtils;
import com.repast.vo.OrdersItemVo;
import com.repast.vo.OrdersVo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/orders/")
public class OrdersController {

	@Autowired
	private IOrdersService orderService;

	// 提交订单
	// 0订单生成成功,1008订单生成失败,1009订单项生成失败
	@RequestMapping("submit_order.do")
	@ResponseBody
	public ServerResponse submitOrder(String json) throws Exception {
		OrdersVo ordersVo = new OrdersVo();
		JSONObject jsonOrders = JSONObject.fromObject(json);
		ordersVo.setOpenid(jsonOrders.getString("openid"));
		ordersVo.setOrdersType(jsonOrders.getInt("ordersType"));
		
		//预约点餐的情况所需要的字段
		ordersVo.setAppointId(jsonOrders.getInt("appointId"));
		
		ordersVo.setOrdersTotal(new Double(jsonOrders.getString("ordersTotal")));
		ordersVo.setDeskId(jsonOrders.getInt("deskId"));
		String ordersTastes = jsonOrders.getString("ordersTastes");
		ordersVo.setOrdersTastes(ordersTastes);
		JSONArray jsonArray = jsonOrders.getJSONArray("ordersItemList");
		List<OrdersItemVo> list = JSONArray.toList(jsonArray, OrdersItemVo.class);
		ordersVo.setOrdersItemList(list);
		return orderService.submitOrder(ordersVo);
	}

	// 0表示修改状态成功,1010订单编号异常,找不到订单,1011设置桌子使用时间失败,1015订单更新失败
	// 支付成功后,修改订单状态,添加deskTime里面的预约时间
	@RequestMapping("success.do")
	@ResponseBody
	public ServerResponse afterSuccess(String ordersNum,
			@RequestParam(value = "appointId", defaultValue = "-1")Integer appointId) {
		return orderService.updateOrders(ordersNum,appointId);
	}
	
	//插叙 最新的5条订单数据,不分类别
	@RequestMapping("list.do")
	@ResponseBody
	public ServerResponse selectOrdersList(String openid,
			@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
			@RequestParam(value = "pageSize", defaultValue = "5")Integer pageSize){
		return orderService.selectOrdersList(openid,pageNum,pageSize);
	}

}
